# -*- coding: utf-8 -*-

import hashlib
import random
from base import UserCase, Runner, TestSuite

class Case(UserCase):
    def rand_aggre(self):
        return random.choice(['avg', 'max', 'min'])

    def fact_create(self):
        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'table': self.randchar(8),
        }
        res = self.c.open('/analytics/fact/create', p)
        self.assertEqual(res['code'], 0)
        return res['data']['code']

    def metric_create(self):
        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'aggre': self.rand_aggre(),
            'column': self.randchar(12),
        }
        res = self.c.open('/analytics/metric/create', p)
        self.assertEqual(res['code'], 0)
        return res['data']['code']

    def dimension_create(self):
        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'column': self.randchar(12),
        }
        res = self.c.open('/analytics/dimension/create', p)
        self.assertEqual(res['code'], 0)
        return res['data']['code']

    def calculate_metric_create(self):
        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'expression': self.randchar(48),
        }
        res = self.c.open('/analytics/calculate-metric/create', p)
        self.assertEqual(res['code'], 0)
        return res['data']['code']

    def test_query(self):
        metric = [self.metric_create()]
        dimension = [self.dimension_create()]
        p = {
            'fact': self.fact_create(),
            'metric': ','.join(metric),
            'dimension': ','.join(dimension),
            'calculateMetric': self.calculate_metric_create(),
            'limit': self.randnum(3),
            'offset': self.randnum(3),
            'orderBy': metric[0],
            'orderDesc': '1',
            'where': '{}~=~a'.format(dimension[0]),
            'havingMetric': '{}~>~100'.format(metric[0]),
        }
        res = self.c.open('/analytics/query', p)
        self.assertNotEqual(res['code'], 0)


    def test_query_corrent(self):
        '正确的一个查询'

        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'table': 'monitor_v2',
        }
        res = self.c.open('/analytics/fact/create', p)
        self.assertEqual(res['code'], 0)
        fact = res['data']['code']


        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'column': 'gmt_date',
        }
        res = self.c.open('/analytics/dimension/create', p)
        self.assertEqual(res['code'], 0)
        dimension = res['data']['code']


        p = {
            'code': self.randchar(12),
            'name': u'最高温度',
            'aggre': 'max',
            'column': 'temp1'
        }
        res = self.c.open('/analytics/metric/create', p)
        self.assertEqual(res['code'], 0)
        metric = res['data']['code']

        p = {
            'fact': fact,
            'dimension': dimension,
            'metric': metric,
            'orderBy': dimension,
            'orderDesc': '1',
        }
        res = self.c.open('/analytics/query', p)
        self.assertEqual(res['code'], 0)


    def test_query_corp(self):

        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'table': 'monitor_v2',
        }
        res = self.c.open('/analytics/fact/create', p)
        self.assertEqual(res['code'], 0)
        fact = res['data']['code']


        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'column': 'year',
        }
        res = self.c.open('/analytics/dimension/create', p)
        self.assertEqual(res['code'], 0)
        dimension = res['data']['code']


        p = {
            'code': self.randchar(12),
            'name': u'最高温度',
            'aggre': 'max',
            'column': 'temp1'
        }
        res = self.c.open('/analytics/metric/create', p)
        self.assertEqual(res['code'], 0)
        metric = res['data']['code']

        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        p = {
            'fact': fact,
            'dimension': dimension,
            'metric': metric,
            'orderBy': dimension,
            'orderDesc': '1',
        }
        res = self.c.open('/analytics/query', p)
        self.assertEqual(res['code'], 0)


test_list = [
    'test_query',
    'test_query_corrent',
    'test_query_corp',
]
Suite = TestSuite([Case(t) for t in test_list])


if __name__ == '__main__':
    Runner.run(Suite)



